<?php
	session_start();
	set_time_limit(0);
	include_once('init.php');
	try {
	$m  = new Mongo();
	} catch(Exception $e) {
	  echo "连接数据库失败:".$e->getMessage();	
	}
	$db = $m->selectDB('foobar');
	$collection = $db->selectCollection("test2");
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>PHP 操作 mongodb 数据库 by 此题无解</title>
		<meta charset="UTF-8">
		<meta name=description content="">
		<meta name=viewport content="width=device-width, initial-scale=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<!-- Bootstrap CSS -->
		<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
		<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
	</head>
	<body>
	<h1 class="text-center">PHP 操作 mongodb 数据库(查询)</h1>
	<div class="container">
		<div class="row">
		1. 插入测试数据
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'inserttest2.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$data = array('title'=>'Aritcle One','author'=>'john','age'=>18,'content'=>'hello');
			$data1 = array('title'=>'Aritcle One','author'=>'jim','age'=>28,'content'=>'hello1');
			$data2 = array('title'=>'Aritcle One','author'=>'tom','age'=>38,'content'=>'hello2');
			$data3 = array('title'=>'Aritcle One','author'=>'lily','age'=>48,'content'=>'hello3');
			$data4 = array('title'=>'Aritcle One','author'=>'tom','age'=>58,'content'=>'hello4');
			$data5 = array('title'=>'Aritcle One','author'=>'jim','age'=>68,'content'=>'hello5');
			$collection->remove();
			$collection->insert($data);
			$collection->insert($data1);
			$collection->insert($data2);
			$collection->insert($data3);
			$collection->insert($data4);
			$collection->insert($data5);
			$cursor = $collection->find();
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(1);?></pre>
		</div>

		2. find 第一个参数指定搜索条件，可以有多个，第二个参数指定显示字段
		查找author=jim，content=hello5的数据，并不显示_id
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'find1.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$cursor = $collection->find(
				array('author'=>'jim','content'=>'hello5'),
				array('_id'=>0,'age'=>0)
				);
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(2);?></pre>
		</div>
		
		3. $lt,$lte,$gt,$gte 比较操作符，可以限定查询结果在某个范围
		查找年龄在20-40之间的记录
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'find5.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$cursor = $collection->find(
				array('age'=>array('$gte'=>20,'$lte'=>40)),
				array('_id'=>0,'title'=>0)
				);
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(3);?></pre>
		</div>

		4. $or $in $nin <br>
		查询年龄在18或68中，或者用户名为lily
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'find3.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$cursor = $collection->find(
				array(
					'$or'=>array(
						array('age'=>array('$in'=>array(18,68))),
						array('author'=>'lily')
						)
					),
				array('_id'=>0,'title'=>0)
				);
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(3);?></pre>
		</div>

		5. $not 取反，可以加在任何其它条件上<br>
		查询年龄在 集合(18,28,68)中取反，或者用户名为lily，实际有$nin可用 
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'find4.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$cursor = $collection->find(
				array(
					'$or'=>array(
						array('age'=>
							array('$not'=>array('$in'=>array(18,28,68)))
							),
						array('author'=>'lily')
						)
					),
				array('_id'=>0,'title'=>0)
				);
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(3);?></pre>
		</div>

		7. 正则匹配 查找名字以li或to开始的记录,正则通过new MongoRegex创建	
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'find7.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result= array();
			$cursor = $collection->find(
				array('author'=>new MongoRegex('/^[li|to]/i')),
				array('_id'=>0,'title'=>0)
				);
			while ($data = $cursor->getNext())
			$result[] = $data;
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(3);?></pre>
		</div>

		6. 查询键值为null的记录，需要确实键确实存在，而且为null<br>
		没有成功插入null，没有测试
		<pre class="prettyprint linenums Lang-php">
			$cursor = $collection->find(
				array(
					'z'=>array('$in'=>array(null),'$exists'=>1)
					),
				);
		</pre>


		<div><blockquote><em>php</em>操作<em>mongodb</em>数据库入门篇(4)<br>
		<a href="fifth.php">下一篇</a>
		<small class="pull-right"><cite>此题无解</cite></small></blockquote></div>

		</div>
	</div>

		<!-- jQuery -->
		<script src="./js/jquery.min.js"></script>
		<!-- Bootstrap JavaScript -->
		<script src="./js/bootstrap.min.js"></script>
		<!-- prettify -->
		<script src="./js/prettify.js"></script>
		<script>
		$(function(){
			prettyPrint();
		});
		</script>
	</body>
</html>